<?php

class HosTools
{
	function __construct()
	{
	}

	static $_instance = null;
	static function getInstance()
	{
		if( self::$_instance === null )
		{
			self::$_instance = new self();
		}
		return self::$_instance;
	}
    
    function info( $obj )
    {
        Module::getInstance()->info( $obj );
    }        
    
    function selectInfo( $ids, &$newsInfo )
    {
        $arr = array();
        if( $ids ) foreach( $ids as $id )
        {
            $id = stripslashes( $id );
            $arr[] = "'{$id}'";
        }
        $sql = "select * from t_house where id in (".implode(',', $arr ).")";
        $r = DBTls::getInstance()->select( $sql, $results );
        if( $r && $r['ret'] )
        {
            $tmp = $results;
            if( $tmp ) foreach( $tmp as $one )
            {
                $newsInfo[$one['id']] = $one;
            }
        }
        else
        {
            $this->info( "select failed.sql:{$sql},ret=".print_r( $r, true ) );
        }
        return $r;
    }
    
    function storeInfo( $infos )
    {
        if( $infos ) foreach( $infos as $one )
        {
            $r = $this->storeOne( $one );
            if( !$r || !$r['ret'] )
            {
                throw new Exception( "update error: ".serialize( $one ) );
            }
        }
        return true;
    }    
    
    function storeOne( $one )
    {
        $sql = "select * from t_house where id='{$one['id']}'";
        $results = array();
        $r = DBTls::getInstance()->select( $sql, $results );
        if( $r['ret'] && $results )
        {
            $id = $one['id'];
            unset( $one['id'] );
            $arr = array();
            foreach( $one as $k=>$v )
            {
                $v = mysql_escape_string( $v );
                $v = addslashes( $v );
                $arr[] = "{$k}='{$v}'";
            }
            
            $sql = "update t_house set ".implode( ',', $arr )." where id='{$id}'";
            $r = DBTls::getInstance()->update( $sql );
            return $r;
        }
        else
        {
            $fields = array();
            $values = array();
            $tostore = array( 'id', 'url', 'img' , 'info', 'price', 'time', 'desc' );
            foreach( $tostore as $fid )
            {
                if( isset( $one[$fid] ) )
                {
                    $one[$fid] = mysql_escape_string( $one[$fid] );
                    $one[$fid] = addslashes( $one[$fid] );
                    $fields[] = $fid;
                    $values[] = "'" . $one[$fid] . "'";
                }
            }
            
            $sql = "insert into t_house (".implode( ',', $fields ).") values(".implode( ',', $values ).")";
            $r = DBTls::getInstance()->insert( $sql );
            return $r;
        }
    }
    
}
